﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Project_2._2.Models;
using Project_2._2.ViewModels;
using System.IO;
using Project_2._2.Extra;
using System.Web.Security;
using PagedList;
using PagedList.Mvc;

namespace Project_2._2.Controllers.AdminControllers
{
    public class AdminRoleController : Controller
    {
        private QuanLyThueXeEntities db = new QuanLyThueXeEntities();
        private Common cm = new Common();
        #region Default
        // GET: /Adminrole/
        public ActionResult Default(int? page, string rd, string Record, string key, string SBy, string SType)
        {
           
            try
            {
                if (!Request.IsAuthenticated)
                {
                    return RedirectToAction("Login", "Admin");
                }
                else
                {
                    if (cm.CheckUserType(User.Identity.Name))
                    {
                        if (rd != null)
                        {
                            int soDong = int.Parse(rd);
                            string k = key;
                            string b = SBy;
                            string t = SType;
                            IPagedList<Role> role = null;
                            if (!string.IsNullOrEmpty(k) && b != "0")
                            {
                                ViewBag.Key = k;
                                ViewBag.SBy = b;
                                ViewBag.SType = t;

                                switch (b)
                                {
                                    case "Name":
                                        if (t == "1")
                                            role = (from p in db.Roles where p.TenRole.Contains(key) && p.isDelete == false select p).OrderByDescending(m => m.RoleId).ToList().ToPagedList(page ?? 1, soDong);
                                        else
                                        {
                                            role = (from p in db.Roles where p.TenRole.Equals(key) && p.isDelete == false select p).OrderByDescending(m => m.RoleId).ToList().ToPagedList(page ?? 1, soDong);
                                        }
                                        break;
                                }
                            }
                            else
                            {
                                role = (from p in db.Roles where p.isDelete == false select p).OrderByDescending(m => m.RoleId).ToList().ToPagedList(page ?? 1, soDong);
                            }

                            ViewBag.record = rd;
                            return View(role);
                        }
                        else
                        {
                            var role = (from p in db.Roles where p.isDelete == false select p).OrderByDescending(m => m.RoleId).ToList().ToPagedList(page ?? 1, 10);
                            return View(role);
                        }
                    }
                    else
                        return RedirectToAction("Login", "Admin");
                }
            }
            catch (Exception)
            {
                return RedirectToAction("SystemErrors", "Default");
            }
        }
        //POST
        [HttpPost]
        public ActionResult Default(FormCollection col, int? page)
        {
            try
            {
                int soDong = int.Parse(col["soDong"]);
                string SearchBy = col["SearchBy"];
                string SearchType = col["SearchType"];
                string key = col["KeyWorld"];
                IPagedList<Role> role = null;

                //
                ViewBag.Key = key;
                ViewBag.SBy = SearchBy;
                ViewBag.SType = SearchType;
                ViewBag.record = soDong.ToString();

                if (string.IsNullOrEmpty(key))
                {
                    role = (from p in db.Roles where p.isDelete == false select p).OrderByDescending(m => m.RoleId).ToList().ToPagedList(page ?? 1, soDong);
                    return View(role);
                }
                else
                {

                    switch (SearchBy)
                    {

                        case "Name":
                            if (SearchType == "1")
                                role = (from p in db.Roles where p.TenRole.Contains(key) && p.isDelete == false select p).OrderByDescending(m => m.RoleId).ToList().ToPagedList(page ?? 1, soDong);
                            else
                            {
                                role = (from p in db.Roles where p.TenRole.Equals(key) && p.isDelete == false select p).OrderByDescending(m => m.RoleId).ToList().ToPagedList(page ?? 1, soDong);
                            }
                            break;
                    }
                    return View(role);
                }
            }
            catch (Exception)
            {
                return RedirectToAction("SystemErrors", "Default");
            }

        }
        #endregion
        public ActionResult Create() 
        {
            if (!Request.IsAuthenticated)
            {
                return RedirectToAction("Login", "Admin");
            }
            else
            {
                if (cm.CheckUserType(User.Identity.Name))
                {
                    return View();
                }
                else
                    return RedirectToAction("Login", "Admin");
            }
        }
        [HttpPost]
        public ActionResult Create(RoleViewModel role)
        {
            if (!Request.IsAuthenticated)
            {
                return RedirectToAction("Login", "Admin");
            }
            else
            {
                if (cm.CheckUserType(User.Identity.Name))
                {
                    try
                    {

                        if (ModelState.IsValid)
                        {

                            if (role.captcha.CompareTo(Session["Captcha"]) != 0)
                                ModelState.AddModelError("Captcha", "Mã xác nhận không chính xác.");
                            else
                            {

                                Role dx = new Role();
                                dx.TenRole = role.RoleName;
                                dx.MoTa = role.MoTa;
                                dx.isDelete = false;

                                db.Roles.Add(dx);
                                db.SaveChanges();

                                return RedirectToAction("Default", "AdminRole");

                            }
                        }
                    }
                    catch (Exception)
                    {
                        return RedirectToAction("SystemErrors", "Default");
                    }
                }
                else
                    return RedirectToAction("Login", "Admin");
            }
            return View();
                
        }
        public ActionResult Update(int id)
        {
            RoleViewModel role_model;
            if (!Request.IsAuthenticated)
            {
                return RedirectToAction("Login", "Admin");
            }
            else
            {
                if (cm.CheckUserType(User.Identity.Name))
                {
                    try
                    {
                        var role = (from p in db.Roles where p.RoleId == id select p).FirstOrDefault();
                        role_model = new RoleViewModel();
                        role_model.RoleId = role.RoleId;
                        role_model.RoleName = role.TenRole;
                        role_model.MoTa = role.MoTa;
                        
                    }
                    catch (Exception)
                    {
                        return RedirectToAction("SystemErrors", "Default");
                    }
                }
                else
                    return RedirectToAction("Login", "Admin");
            }
            return View(role_model);
        }
        [HttpPost]
        public ActionResult Update(RoleViewModel role)
        {
            if (!Request.IsAuthenticated)
            {
                return RedirectToAction("Login", "Admin");
            }
            else
            {
                if (cm.CheckUserType(User.Identity.Name))
                {
                    try
                    {
                        if (ModelState.IsValid)
                        {

                            if (role.captcha.CompareTo(Session["Captcha"]) != 0)
                                ModelState.AddModelError("Captcha", "Mã xác nhận không chính xác.");
                            else
                            {
                                var dx = (from p in db.Roles where p.RoleId == role.RoleId select p).FirstOrDefault();
                                dx.TenRole = role.RoleName;
                                dx.MoTa = role.MoTa;
                                dx.isDelete = false;
                                UpdateModel(dx);
                                db.SaveChanges();
                                return RedirectToAction("Default", "AdminRole");
                            }
                        }
                    }
                    catch (Exception)
                    {
                        return RedirectToAction("SystemErrors", "Default");
                    }
                   
                }
                else
                    return RedirectToAction("Login", "Admin");
            }
            return View();
        }
        public ActionResult Delete(int id)
        {
            if (!Request.IsAuthenticated)
            {
                return RedirectToAction("Login", "Admin");
            }
            else
            {
                if (cm.CheckUserType(User.Identity.Name))
                {
                    try
                    {
                        Role role = (from p in db.Roles where p.RoleId == id select p).First();
                        role.isDelete = true;
                        UpdateModel(role);
                        db.SaveChanges();
                        return RedirectToAction("Default", "AdminRole");
                    }
                    catch (Exception)
                    {
                        return RedirectToAction("SystemErrors", "Default");
                    }

                }
                else
                    return RedirectToAction("Login", "Admin");
            }
        }
        [HttpPost]
        public ActionResult Multi_Delete(IEnumerable<int> RoleIdsToDelete)
        {
            if (!Request.IsAuthenticated)
            {
                return RedirectToAction("Login", "Admin");
            }
            else
            {
                if (cm.CheckUserType(User.Identity.Name))
                {
                    try
                    {
                        var Role_delete = db.Roles.Where(x => RoleIdsToDelete.Contains(x.RoleId)).ToList();
                        foreach (var role in Role_delete)
                        {
                            role.isDelete = true;
                            UpdateModel(role);
                            db.SaveChanges();
                        }
                        return RedirectToAction("Default", "AdminRole");
                    }
                    catch (Exception)
                    {
                        return RedirectToAction("SystemErrors", "Default");
                    }
                }
                else
                    return RedirectToAction("Login", "Admin");
            }
          
        }
    }
}
